package com.yc.yyds.servlet;

import com.google.gson.Gson;
import com.yc.yyds.Bean.Result;
import com.yc.yyds.dao.Moviedao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SaveMovieServlet", value = "/SaveMovieServlet.s")
public class SaveMovieServlet extends HttpServlet {
    Moviedao dao=new Moviedao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String moviename = request.getParameter("moviename");
        String moviedesc = request.getParameter("moviedesc");
        String moviegrade = request.getParameter("moviegrade");
        String movieimage = request.getParameter("movieimage");
        String moviedate = request.getParameter("moviedate");
        String area=request.getParameter("area");
        String type = request.getParameter("type");
        String movieid = request.getParameter("movieid");
        System.out.println("movieimage = " + movieimage);
        Result result;
        try{
           if(movieid!=null){
               dao.modify(movieid,moviename,moviedesc,moviegrade,movieimage,moviedate,area,type);
           }else{
               dao.create(moviename,moviedesc,moviegrade,movieimage,moviedate,area,type);
           }
            result=new Result(1,"图书保存成功",null);
        }catch ( Exception e){
            e.printStackTrace();
            result=new Result(0,e.getMessage(),null);
        }
        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }
}
